#include "main.h"

extern SDL_Event event;

extern SDL_Rect camera;

//The surfaces
extern SDL_Surface *screen;
extern SDL_Surface *player;

//Player sprite from character sprite sheet
extern SDL_Rect clipsRight[ PLAYER_ANIMATIONS ];
extern SDL_Rect clipsLeft[ PLAYER_ANIMATIONS ];
extern SDL_Rect clipsUp[ PLAYER_ANIMATIONS ];
extern SDL_Rect clipsDown[ PLAYER_ANIMATIONS ];

Player::Player()
{
    //Initialize the offsets
    box.x = PLAYER_X_POS;
    box.y = PLAYER_Y_POS;
    box.w = PLAYER_WIDTH;
    box.h = PLAYER_HEIGHT;

    //Initialize the velocity
    xVel = 0;
    yVel = -PLAYER_VELOCITY;
    
    //Initialize the animation variables
    frame = 0;
    status = PLAYER_UP;
}

void Player::handle_input()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel = -PLAYER_VELOCITY;  xVel = 0; break;
            case SDLK_DOWN: yVel = PLAYER_VELOCITY; xVel = 0; break;
            case SDLK_LEFT: xVel = -PLAYER_VELOCITY; yVel = 0; break;
            case SDLK_RIGHT: xVel = PLAYER_VELOCITY; yVel = 0; break;
        }
    }
    //If a key was released
    /*else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += PLAYER_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= PLAYER_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += PLAYER_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= PLAYER_WIDTH / 2; break;
        }
    }*/
}

void Player::move( Tile *tiles[] )
{
    //Move the player left or right
    box.x += xVel;

    //If the player went too far to the left or right or touched a wall
    if( ( box.x < 0 ) || ( box.x + PLAYER_WIDTH > LEVEL_WIDTH ) || touches_wall( box, tiles ) )
    {
        //move back
        box.x -= xVel;
    }

    //Move the player up or down
    box.y += yVel;

    //If the player went too far up or down or touched a wall
    if( ( box.y < 0 ) || ( box.y + PLAYER_HEIGHT > LEVEL_HEIGHT ) || touches_wall( box, tiles ) )
    {
        //move back
        box.y -= yVel;
    }
}

void Player::show()
{
    //Show the player
    //apply_surface( box.x - camera.x, box.y - camera.y, player, screen );
    //If Foo is moving left
    if( xVel < 0 )
    {
        //Set the animation to left
        status = PLAYER_LEFT;

        //Move to the next frame in the animation
        frame++;
    }
    //If Foo is moving right
    else if( xVel > 0 )
    {
        //Set the animation to right
        status = PLAYER_RIGHT;

        //Move to the next frame in the animation
        frame++;
    }
    else if( yVel > 0 )
    {
        //Set the animation to right
        status = PLAYER_DOWN;

        //Move to the next frame in the animation
        frame++;
    }
    else if( yVel < 0 )
    {
        //Set the animation to right
        status = PLAYER_UP;

        //Move to the next frame in the animation
        frame++;
    }
    //If Foo standing
    else
    {
        //Restart the animation
        frame = 0;
    }

    //Loop the animation
    if( frame >= 3 )
    {
        frame = 0;
    }

    //SDL_SetAlpha( player, 0x00, 0 );
    //Show the stick figure
    if( status == PLAYER_RIGHT )
    {
        apply_surface( box.x - camera.x, box.y - camera.y, player, screen, &clipsRight[ frame ], 0 );
    }
    else if( status == PLAYER_LEFT )
    {
        apply_surface( box.x - camera.x, box.y - camera.y, player, screen, &clipsLeft[ frame ], 0 );
    }
    else if( status == PLAYER_UP )
    {
        apply_surface( box.x - camera.x, box.y - camera.y, player, screen, &clipsUp[ frame ], 0 );
    }
    else if( status == PLAYER_DOWN )
    {
        apply_surface( box.x - camera.x, box.y - camera.y, player, screen, &clipsDown[ frame ], 0 );
    }
}

void Player::set_camera()
{
    //Center the camera over the player
    camera.x = ( box.x + PLAYER_WIDTH / 2 ) - SCREEN_WIDTH / 2;
    camera.y = ( box.y + PLAYER_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;

    //Keep the camera in bounds.
    if( camera.x < 0 )
    {
        camera.x = 0;
    }
    if( camera.y < 0 )
    {
        camera.y = 0;
    }
    if( camera.x > LEVEL_WIDTH - camera.w )
    {
        camera.x = LEVEL_WIDTH - camera.w;
    }
    if( camera.y > LEVEL_HEIGHT - camera.h )
    {
        camera.y = LEVEL_HEIGHT - camera.h;
    }
}

SDL_Rect Player::getBox()
{
   return box;
}
